function modulated_symbols = modulate_symbols(data_bits, modulation_type)
% modulate_symbols - 对输入比特进行数字调制
%
% 输入参数:
%   data_bits       - 需要调制的二进制数据
%   modulation_type - 调制类型: 'BPSK', 'QPSK', 'QAM16', 'QAM64'
%
% 输出参数:
%   modulated_symbols - 调制后的符号

switch upper(modulation_type)
    case 'BPSK'
        % BPSK调制: 0 -> -1, 1 -> 1
        modulated_symbols = 2 * data_bits - 1;
        
    case 'QPSK'
        % QPSK调制: 每两个比特映射为一个复符号
        if mod(length(data_bits), 2) ~= 0
            % 确保比特数是偶数
            data_bits = [data_bits 0];
            warning('QPSK调制需要偶数个比特，已在末尾填充一个0');
        end
        
        % 重塑为每行2比特的矩阵
        bit_pairs = reshape(data_bits, 2, [])';
        
        % 初始化调制符号
        num_symbols = size(bit_pairs, 1);
        modulated_symbols = zeros(1, num_symbols);
        
        % QPSK调制映射
        for i = 1:num_symbols
            switch bin2dec(char(bit_pairs(i, :) + '0'))
                case 0  % 00
                    modulated_symbols(i) = (-1 - 1i) / sqrt(2);
                case 1  % 01
                    modulated_symbols(i) = (-1 + 1i) / sqrt(2);
                case 2  % 10
                    modulated_symbols(i) = (1 - 1i) / sqrt(2);
                case 3  % 11
                    modulated_symbols(i) = (1 + 1i) / sqrt(2);
            end
        end
        
    case 'QAM16'
        % 16QAM调制: 每四个比特映射为一个复符号
        if mod(length(data_bits), 4) ~= 0
            % 确保比特数是4的倍数
            padding = 4 - mod(length(data_bits), 4);
            data_bits = [data_bits zeros(1, padding)];
            warning('16QAM调制需要4的倍数个比特，已在末尾填充%d个0', padding);
        end
        
        % 重塑为每行4比特的矩阵
        bit_quads = reshape(data_bits, 4, [])';
        
        % 初始化调制符号
        num_symbols = size(bit_quads, 1);
        modulated_symbols = zeros(1, num_symbols);
        
        % 16QAM调制映射
        for i = 1:num_symbols
            decimal = bin2dec(char(bit_quads(i, :) + '0'));
            
            % 确定实部和虚部（格雷码映射）
            % 前两位确定实部，后两位确定虚部
            real_part = 2 * bit_quads(i, 1) - 1;
            imag_part = 2 * bit_quads(i, 3) - 1;
            
            % 根据第二位和第四位调整幅度
            if bit_quads(i, 2) == 0
                real_part = real_part * 3;
            end
            if bit_quads(i, 4) == 0
                imag_part = imag_part * 3;
            end
            
            % 归一化能量
            modulated_symbols(i) = (real_part + 1i*imag_part) / sqrt(10);
        end
        
    case 'QAM64'
        % 64QAM调制: 每六个比特映射为一个复符号
        if mod(length(data_bits), 6) ~= 0
            padding = 6 - mod(length(data_bits), 6);
            data_bits = [data_bits zeros(1, padding)];
            warning('64QAM调制需要6的倍数个比特，已在末尾填充%d个0', padding);
        end
        
        % 重塑为每行6比特的矩阵
        bit_groups = reshape(data_bits, 6, [])';
        
        % 初始化调制符号
        num_symbols = size(bit_groups, 1);
        modulated_symbols = zeros(1, num_symbols);
        
        % 64QAM调制映射（简化版）
        for i = 1:num_symbols
            % 前3位映射到实部，后3位映射到虚部
            I_bits = bit_groups(i, 1:3);
            Q_bits = bit_groups(i, 4:6);
            
            % 将3位二进制转为十进制（0-7）
            I_dec = bin2dec(char(I_bits + '0'));
            Q_dec = bin2dec(char(Q_bits + '0'));
            
            % 映射到[-7, -5, -3, -1, 1, 3, 5, 7]
            I_val = 2 * I_dec - 7;
            Q_val = 2 * Q_dec - 7;
            
            % 归一化能量
            modulated_symbols(i) = (I_val + 1i*Q_val) / sqrt(42);
        end
        
    otherwise
        error('不支持的调制类型: %s', modulation_type);
end

end 